package uz.edo.app.client.ui.doc;

import java.text.SimpleDateFormat;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import uz.edo.app.client.MyPage;
import uz.edo.db.bean.Registrationdoc;
import uz.edo.db.manager.RegistrationdocManager;

public class DocListView extends MyPage{

    private RegistrationdocManager manager = RegistrationdocManager.getInstance();

    @SuppressWarnings("unchecked")
	public DocListView() {
        try {
            final DataView dataView;
            add(dataView = new DataView("registrationdocs",new ListDataProvider(manager.getAll()),ROWS_PER_PAGE){
				private static final long serialVersionUID = 1L;

				@Override
                protected void populateItem(Item item) {
                    final Registrationdoc registrationdoc = (Registrationdoc)item.getModelObject();
                    item.add(new Label("personfio",registrationdoc.getPersonfio()));
                    item.add(new Label("murojaatDescription",registrationdoc.getMurojaatDescription()));
                    item.add(new Label("murojaatDate",FORMATTER.format(registrationdoc.getMurojaatDate())));
                    item.add(new Link("editLink"){
						private static final long serialVersionUID = 1L;
						@Override
                        public void onClick() {
                            setResponsePage(new RegistrationDocEdit(registrationdoc));
                        }
                    });
                    item.add(new Link("deleteLink"){
						private static final long serialVersionUID = 3275952915053427000L;
						@Override
                        public void onClick() {
                            try {
                                manager.delete(registrationdoc);
                                setResponsePage(DocListView.class);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            });

            add(new PagingNavigator("navigator", dataView){
				private static final long serialVersionUID = -553129896105266145L;
				public boolean isVisible() {
                    return dataView.getPageCount() > 1;
                }
            });

            add(new Link("addLink") {
				private static final long serialVersionUID = -5390081226712628659L;
				@Override
                public void onClick() {
			        setResponsePage(RegistrationDocAdd.class);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
